﻿using System;
using System.Drawing;
using System.IO;
using System.Windows;
using WinForm = System.Windows.Forms;

namespace SHomeWorkshop.LunarPresentation.Commands
{
    public partial class Command
    {
        /// <summary>
        /// 如果当前公式是图片框，则会将剪贴板中的图像数据粘贴进这个图片框；
        /// 如果当前公式不是图片框，则会自动新插入一个图片框，并将剪贴板中的图像数据粘贴进这个新插入的图片框。
        /// </summary>
        public static void PasteImageFromClipboard()
        {
            if (Globals.selectedEditor == null) return;
            if (Globals.selectedEditor.SelectedExpression == null) return;

            LunarPresentation.Expressions.Expression curEx
                = Globals.selectedEditor.SelectedExpression;

            PasteImageFromClipboard(curEx);
        }

        public static bool PasteImageFromClipboard(Expressions.Expression ex)
        {
            if (ex == null) return false;

            Expressions.PictureBox pictureBox = null;

            WinForm.IDataObject oDataObj = WinForm.Clipboard.GetDataObject();
            if (oDataObj != null)
            {
                if (oDataObj.GetDataPresent(DataFormats.Bitmap))
                {
                    Image oImgObj = oDataObj.GetData(DataFormats.Bitmap, true) as Image;

                    using (MemoryStream stm = new MemoryStream())
                    {
                        if (oImgObj == null)
                        {
                            MessageBox.Show("未能顺利取出剪贴板中的图像数据！"
                                + "请将图片加工一下或另选一张图片。",
                                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                            return false;
                        }

                        // 准备“撤销”与“重做”所需要的数据。
                        ModifingItem mi = new ModifingItem();

                        if (ex is Expressions.PictureBox)
                        {
                            pictureBox = ex as Expressions.PictureBox;
                        }
                        else
                        {
                            if (ex is Expressions.SubPanel)
                            {
                                MessageBox.Show("请另选一个公式，不能在子面板上粘贴图片。",
                                Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                                return false;
                            }

                            pictureBox = Command.InsertPictureBox();

                            #region 只有插入新的图片框才需要这些Action。
                            int pictureBoxIndex = pictureBox.ParentPanel.BasePanel.Children.IndexOf(pictureBox);
                            Type newExType = pictureBox.GetType();

                            Action insertEx = new Action(pictureBox.ExID, pictureBoxIndex, newExType.Name,
                                "InsertExpression", null, pictureBox.XmlData.OuterXml, ActionType.Insert, ex.ExID);

                            mi.Add(insertEx);

                            Action indentEx = new Action(pictureBox.ExID, pictureBoxIndex, newExType.Name,
                                "Indent", pictureBox.Indent.ToString(), pictureBox.Indent.ToString());
                            mi.Add(indentEx);
                            #endregion
                        }

                        if (pictureBox == null)
                        {
                            MessageBox.Show("发生意外，未能自动添加新的图片框式。",
                                Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                            return false;
                        }

                        oImgObj.Save(stm, System.Drawing.Imaging.ImageFormat.Png);//默认以png格式保存
                        if (stm.Length <= 512000)
                        {
                            byte[] b = stm.GetBuffer();
                            string imgDataText = Convert.ToBase64String(b);

                            System.Xml.XmlNode imgNode = pictureBox.XmlData.SelectSingleNode("Image");

                            #region 准备通用的“撤销”与“重做”数据。
                            Action action = new Action(pictureBox.ExID,
                                pictureBox.ParentPanel.BasePanel.Children.IndexOf(pictureBox),
                                typeof(Expressions.PictureBox).Name, "ImageBase64", imgNode.InnerText, imgDataText);
                            mi.Add(action);
                            #endregion

                            imgNode.InnerText = imgDataText;
                            pictureBox.BuildImage();

                            //加入“撤销”列表。
                            pictureBox.Editor.Do(mi);

                            pictureBox.Img.Width = oImgObj.Width;
                            pictureBox.Img.Height = oImgObj.Height;

                            pictureBox.Editor.IsModified = true;
                            return true;
                        }
                        else
                        {
                            MessageBox.Show("为防止文档体积过分庞大，图片框只支持文件尺寸在 500KB 以下的图片！"
                                + "请将图片加工一下或另选一张图片。",
                                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                            return false;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("并未在剪贴板中找到可粘贴的图片数据！",
                        Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                    return false;
                }
            }
            else { return false; }
        }
    }
}
